package stack;



public class Begin1 {
    public static void main(String[] args) {
        Linked linked = new Linked();
        linked.pushData(new Data(1));
        linked.pushData(new Data(2));
        linked.pushData(new Data(3));
        linked.popData();
        linked.showList();
    }
}
class Data{

    private int id;
    private Data next;
    public Data() {

    }

    public Data(int id) {
        this.id = id;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public Data getNext() {
        return next;
    }

    public void setNext(Data next) {
        this.next = next;
    }
}
class Linked{
    private Data head= new Data();
    private Data temp;
    public void pushData(Data data){
        if(head.getNext()==null){
            head.setNext(data);
            temp=data;
            System.out.println("111"+temp.getId());
            return;
        }
        data.setNext(temp);
        temp=data;
        head.setNext(data);
    }
    public void popData(){
        if(head.getNext()==null){
            System.out.println("栈空");
            return;
        }
        System.out.println("弹出的数据为:"+head.getNext().getId());
        head.setNext(head.getNext().getNext());
    }
    public void showList(){
        if(head.getNext()==null){
            System.out.println("链表为空");
        }
        //因为头节点是不能动的 所以需要临时遍历辅助
        Data temp = head;
        while (true){
            temp=temp.getNext();
            if (temp==null){
                break;
            }
            System.out.println(temp.getId());
        }
        System.out.println("输出完毕");
    }
}
